package soar.template.springboot.mq.rabbitmq.producer.app;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import soar.template.springboot.mq.rabbitmq.producer.appconfig.AppConfig;
import soar.template.springboot.mq.rabbitmq.producer.appconfig.rabbitmq.Config;

@Component
public class MessageProducer implements CommandLineRunner
{
    private final static Logger logger = LoggerFactory
            .getLogger(MessageProducer.class);

    @Resource(name = "appconfig")
    private AppConfig appconfig;

    @Resource(name = "producerMainRabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    @Override
    public void run(String... args) throws Exception
    {
        try
        {
            for (int i = 0; i < 100; i++)
            {
                String body = "测试" + i;
                Config config = appconfig.rabbitmq.producerMain;
                rabbitTemplate.convertAndSend(config.exchange, config.queue, body);
                logger.info("send({}) to exchange({}) queue({}) successfully",
                        body, config.exchange, config.queue);
            }
        }
        catch (Exception e)
        {
            if (e.getMessage() != null)
                logger.error("fail: {}", e);
        }
        finally
        {
        }

        System.exit(0);
    }
}
